package com.quanquan.store.controller;

import com.quanquan.store.dto.AllOrderDTO;
import com.quanquan.store.request.OrderRequest;
import com.quanquan.store.service.OrderService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import web.response.ResultBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName:OrderController
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/7/29 11:47
 * @Version 1.0
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    private final OrderService orderService;

    @Autowired
    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * 多条件查询
     *
     * @return
     */
    @PostMapping("query_select")
    public Map<String, Object> queryByCondition(OrderRequest requestBody) {
        Integer page = requestBody.getPage();
        if (null != page && page > 0) {
            page = page - 1;
            requestBody.setPage(page);
        }
        List<AllOrderDTO> allOrderDTOS = orderService.findByManagement(requestBody);
        Map<String, Object> data = new HashMap<>();
        if (allOrderDTOS.size() > 0) {
            data.put("size", allOrderDTOS.size());
            data.put("rows", allOrderDTOS);
            data.put("status", 200);
            data.put("msg", "success!");
            data.put("total", orderService.queryBySumCount());
        } else {
            data.put("size", 10);
            data.put("rows", null);
            data.put("status", -200);
            data.put("msg", "no data!");
            data.put("total", 0);
        }
        return data;
    }

    @PostMapping("query_by_coupon_id")
    public ResultBody queryByCouponId(@RequestBody OrderRequest request) {
        List<AllOrderDTO> allOrderDTOS;
        allOrderDTOS = orderService.findByCouponId(request.getCouponId());
        if (allOrderDTOS.size() > 0) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), allOrderDTOS);
        }
        return new ResultBody(ResCodeEnum.NO_DATA.getCode(), ResCodeEnum.NO_DATA.getMsg());

    }

}
